package com.intexh.quickandroid.module.chat.ui.face;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by engine on 14-1-6.
 */
public class FacePageAdapter extends PagerAdapter implements AdapterView.OnItemClickListener {
    Context mContext ;
    private List<View> pages = new ArrayList<View>();
    private FaceItemClickListener itemClickListener;
	private ArrayList<List<Face>> group;

    public FacePageAdapter(Context context, FaceItemClickListener clickListener) {
        this.mContext = context;
        this.itemClickListener = clickListener;
        initPages();
    }

    public void initPages() {
        group = new ArrayList<List<Face>>();
        List<Face> faces = FaceParser.readMap(mContext);
        List<Face> onePageFace = null;
        for (int i = 0; i < faces.size(); i++) {
            if (i % 10 == 0) {
                onePageFace = new ArrayList<Face>();
                group.add(onePageFace);
            }
            onePageFace.add(faces.get(i));
        }
        pages.clear();
//        for (int i = 0; i < group.size(); i++) {
//        	try {
//                GridView gridView = new GridView(mContext);
//                gridView.setOnItemClickListener(this);
//                gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
//                gridView.setNumColumns(5);
//                FaceAdapter adapter = new FaceAdapter(mContext, group.get(i));
//                gridView.setAdapter(adapter);
//                pages.add(gridView);
//			} catch (OutOfMemoryError e) {
//				UmengUtils.countCrash(mContext, e);
//			}
//        }
    }

    public FaceItemClickListener getItemClickListener() {
        return itemClickListener;
    }

    public void setItemClickListener(FaceItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getCount() {
        return group.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        GridView gridView = null;
        if(position < pages.size()){
        	gridView = (GridView)pages.get(position);
        }
        if(gridView == null){
        	try{
	          gridView = new GridView(mContext);
		      gridView.setOnItemClickListener(this);
		      gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		      gridView.setNumColumns(5);
		      FaceAdapter adapter = new FaceAdapter(mContext, group.get(position));
		      gridView.setAdapter(adapter);
		      pages.add(position, gridView);
        	}catch(OutOfMemoryError e){

        	}
        }
        container.addView(pages.get(position));
        return pages.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    	container.removeView(pages.get(position));
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (itemClickListener != null) {
            Face item = (Face)adapterView.getAdapter().getItem(i);
            itemClickListener.onFaceItemClick(item);
        }
    }

    public interface FaceItemClickListener {
        public void onFaceItemClick(Face item);
    }
}
